package org.wenhuayuan.uilibrary.tab.top;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

import org.wenhuayuan.uilibrary.R;
import org.wenhuayuan.uilibrary.tab.common.IHiTab;

/**
 * 单个的tab，这里继承自FrameLayout或者RelativeLayout都时一样的
 */
public class HiTabTop extends FrameLayout implements IHiTab<HiTabTopInfo<?>> {

    private HiTabTopInfo<?> tabInfo;
    private ImageView tabImageView;
    private TextView tabNameView;
    private View indicator;

    public HiTabTop(Context context) {
        this(context, null);
    }

    public HiTabTop(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HiTabTop(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.init();
    }

    @Override
    public void setHiTabInfo(@NonNull HiTabTopInfo<?> info) {
        this.tabInfo = info;
        this.inflateInfo(false, true);
    }

    /** 动态修改某个tab的高度 */
    @Override
    public void resetHeight(int height) {
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = height;
        setLayoutParams(params);
        getTabNameView().setVisibility(View.GONE);
    }

    @Override
    public void onTabSelectedChange(int index, @NonNull HiTabTopInfo<?> prevInfo, @NonNull HiTabTopInfo<?> nextInfo) {
        // 通过HiTabBottomLayout中的listener.onTabSelectedChange()，四次执行。进入到这里，会执行4次循环。
        // 如果被点击的tab(nextInfo)，和上一次被点击的tab(prevInfo)相同 prevInfo == nextInfo，会被拦截
        // 如果被点击的tab和上一次被点击的tab外的其他点击事件。都会进入到该分支被拦截

        // 拦截相同点击tab，及两次不同点击外的其他点击事件。
        if (prevInfo != tabInfo && nextInfo != tabInfo || prevInfo == nextInfo) {
            return;
        }
        if (nextInfo == tabInfo) {
            inflateInfo(true, false); // 本次tab被选中
        } else {
            // 上一次tab被取消选中
            inflateInfo(false, false);
        }
    }

    private void init() {
        // 初始化view，并添加到RelativeLayout中
        LayoutInflater.from(getContext()).inflate(R.layout.hi_tab_top, this);
        tabImageView = findViewById(R.id.iv_image);
        tabNameView = findViewById(R.id.tv_name);
        indicator = findViewById(R.id.tab_top_indicator);
    }

    // 填充视图
    private void inflateInfo(boolean selected, boolean init) {
        if (tabInfo.tabType == HiTabTopInfo.TabType.TEXT) {
            if(init) {
                tabImageView.setVisibility(View.GONE);
                tabNameView.setVisibility(View.VISIBLE);
                if(!TextUtils.isEmpty(tabInfo.name)) {
                    tabNameView.setText(tabInfo.name);
                }
            }

            if (selected) {
                tabNameView.setTextColor(getTextColor(tabInfo.tintColor));
                indicator.setVisibility(View.VISIBLE);
            } else {
                tabNameView.setTextColor(getTextColor(tabInfo.defaultColor));
                indicator.setVisibility(View.GONE);
            }

        } else if (tabInfo.tabType == HiTabTopInfo.TabType.BITMAP) {
           if(init) {
               tabImageView.setVisibility(View.VISIBLE);
               tabNameView.setVisibility(View.GONE);
           }
           if(selected) {
               tabImageView.setImageBitmap(tabInfo.selectedBitmap);
           } else {
               tabImageView.setImageBitmap(tabInfo.defaultBitmap);
           }
        }
    }

    public HiTabTopInfo<?> getTabInfo() {
        return tabInfo;
    }

    public ImageView getTabImageView() {
        return tabImageView;
    }

    public TextView getTabNameView() {
        return tabNameView;
    }

    @ColorInt
    private int getTextColor(Object obj) {
        if(obj instanceof String) {
            return Color.parseColor((String)obj);
        } else {
            return (int)obj;
        }
    }
}
